<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>课程管理</title>
    <link href="/assets/images/favicon.ico" rel="icon">
    <link rel="stylesheet" href="/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/assets/module/admin.css?v=318"/>

    <script type="text/javascript" src="/assets/libs/layui/layui.js"></script>
    <script type="text/javascript" src="/assets/js/common.js?v=318"></script>

    <style>
        /* 搜索栏样式 */
        .search-bar {
            padding: 10px;
        }

        .layui-form-item .layui-inline {
            margin-bottom: 10px;
        }

        /* 二维码弹窗样式 */
        .qr-modal {
            text-align: center;
            padding: 20px;
        }

        .qr-modal img {
            width: 300px; /* 设置合适的宽度 */
            height: 300px; /* 设置合适的高度 */
        }

        .course-detail-modal {
            padding: 20px;  /* 添加内边距，让内容离边框有空间 */
            background-color: #fff;
            font-size: 14px;
        }

        .layui-form-item {
            margin-bottom: 20px; /* 每个输入项之间增加间距 */
        }
    </style>

    <script type="text/javascript">
        var url = window.location.pathname.substring(1);
        var item = url.split("/");
        var C = item[0];
        var A = item[1];
        var cUrl = "/" + C;
    </script>
    <!-- 其他head内容 -->
    <script src="https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
</head>

<body>
<div>
    <div class="layui-card">
        <div class="layui-card-header search-bar">
            <form class="layui-form">
                <div class="layui-form-item">
                    <!-- 项目选择 -->
                    <div class="layui-inline">
                        <label class="layui-form-label">项目</label>
                        <div class="layui-input-inline">
                            <select id="tbProject" name="project" lay-filter="tbProject">
                                <option value="">请选择项目</option>
                            </select>
                        </div>
                    </div>

                    <!-- 时间选择 -->
                    <div class="layui-inline">
                        <label class="layui-form-label">日期</label>
                        <div class="layui-input-inline">
                            <input type="text" id="tbDate" name="date" placeholder="请选择日期"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <!-- 查询按钮 -->
                    <div class="layui-inline">
                        <div class="layui-input-inline" style="width: auto;">
                            <button id="search" class="layui-btn" lay-submit="" lay-filter="searchForm">
                                <i class="layui-icon layui-icon-search"></i> 查询
                            </button>
                        </div>
                    </div>
                    <!-- 批量导入课程按钮 -->
                    <div class="layui-inline">
                        <button id="batchImport" type="button" class="layui-btn layui-btn-normal">
                            <i class="layui-icon layui-icon-upload"></i> 批量导入课程
                        </button>
                    </div>
                    <!-- 新增按钮 -->
                    <div class="layui-inline">
                        <button id="addNew" type="button" class="layui-btn layui-btn-normal">
                            <i class="layui-icon layui-icon-add-circle"></i> 新增
                        </button>
                    </div>
                </div>
            </form>
        </div>

        <div class="layui-card-body">
            <table class="layui-hide" id="tableList" lay-filter="tableList"></table>
            <script type="text/html" id="barTable">
                <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="checkIn">签到</a> <!-- layui-btn-normal 对应绿色 -->
                <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="viewDetail">编辑</a> <!-- layui-btn-primary 对应蓝色 -->
                <a class="layui-btn layui-btn-xs" lay-event="printQRCode">打印</a> <!-- layui-btn-disabled 对应灰色 -->
                <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a> <!-- layui-btn-danger 对应红色 -->
                <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="export">课件</a> <!-- layui-btn-warm 对应橙色 -->
            </script>

            <!-- 课件管理弹窗 -->
            <script type="text/html" id="fileModal">
                <div class="layui-form" style="padding: 20px;">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn layui-btn-normal" id="uploadBtn">
                            <i class="layui-icon layui-icon-upload"></i>上传课件
                        </button>
                        <div class="layui-upload-list">
                            <table class="layui-table">
                                <thead>
                                <tr>
                                    <th>文件名</th>
                                    <th>授课教师</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody id="fileList"></tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </script>
            <!-- 批量导入弹窗模板 -->
            <script type="text/html" id="batchImportModal">
                <div class="layui-form" style="padding: 20px;">
                    <div class="layui-form-item">
                        <label class="layui-form-label">项目</label>
                        <div class="layui-input-block">
                            <select id="importProject" lay-filter="importProject">
                                <option value="">请选择项目</option>
                                <!-- 动态加载项目选项 -->
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">上传文件</label>
                        <div class="layui-input-block">
                            <input type="file" id="importFile" class="layui-input" style="display: none;" accept=".xls,.xlsx"/>
                            <button type="button" class="layui-btn layui-btn-primary" id="chooseFile">
                                <i class="layui-icon layui-icon-upload"></i> 选择文件
                            </button>
                            <span id="fileNameDisplay" style="margin-left: 10px;"></span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" id="uploadFile">上传</button>
                        </div>
                    </div>
                </div>
            </script>
            <script type="text/html" id="qrCodeModal">
                <div class="qr-modal">
                    <img id="qrCodeImage" src="" alt="二维码"/>
                    <!--                    <button class="layui-btn" onclick="layui.layer.closeAll()">关闭</button>-->
                </div>
            </script>
            <script type="text/html" id="courseDetailModal">
                <div class="layui-form" style="padding: 20px;" >
                    <div class="layui-form-item">
                        <label class="layui-form-label">项目</label>
                        <div class="layui-input-block">
                            <select id="detProject" lay-filter="detProject">
                                <option value="">请选择项目</option>
                                <!-- 动态加载项目选项 -->
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">日期</label>
                        <div class="layui-input-block">
                            <input type="text" id="lessonDate" name="date" placeholder="请选择日期" class="layui-input" autocomplete="off">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">星期</label>
                        <div class="layui-input-block">
                            <input type="text" id="week" class="layui-input" autocomplete="off">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">时间段</label>
                        <div class="layui-input-block">
                            <select id="timeSpan" lay-filter="timeSpan">
                                <option value="">请选择时间段</option>
                                <option value="上午">上午</option>
                                <option value="下午">下午</option>
                                <option value="晚上">晚上</option>
                                <option value="全天">全天</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">地点</label>
                        <div class="layui-input-block">
                            <input type="text" id="address"  class="layui-input" autocomplete="off">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">专题</label>
                        <div class="layui-input-block">
                            <input type="text" id="subject"  class="layui-input" autocomplete="off">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">内容要点</label>
                        <div class="layui-input-block">
                            <textarea id="content"  class="layui-textarea" autocomplete="off"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">课程类型</label>
                        <div class="layui-input-block">
                            <input type="text" id="classType"  class="layui-input" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">培训方式</label>
                        <div class="layui-input-block">
                            <input type="text" id="classMode"  class="layui-input"  autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">授课教师</label>
                        <div class="layui-input-block">
                            <input type="text" id="teacher"  class="layui-input"  autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">教师单位</label>
                        <div class="layui-input-block">
                            <input type="text" id="department" class="layui-input"  autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">职称</label>
                        <div class="layui-input-block">
                            <input type="text" id="title"  class="layui-input"  autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">省内外专家</label>
                        <div class="layui-input-block">
                            <input type="text" id="region"  class="layui-input"  autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">学时</label>
                        <div class="layui-input-block">
                            <input type="text" id="classHours" class="layui-input"  autocomplete="off">
                        </div>
                    </div>
                </div>
            </script>

            <script th:inline="none">
                layui.use(['table', 'form', 'layer', 'laydate', 'jquery','upload'], function () {
                    var $ = layui.jquery;
                    var layer = layui.layer;
                    var form = layui.form;
                    var table = layui.table;
                    var laydate = layui.laydate;
                    var upload = layui.upload;
                    // 初始化日期选择器
                    laydate.render({
                        elem: '#tbDate',
                        type: 'date'  // 选择具体日期
                    });



                    // 页面加载时加载项目列表
                    loadProjectList();

                    // 加载项目列表
                    function loadProjectList() {
                        return new Promise((resolve, reject) => {
                            console.log("加载项目列表");
                            $.ajax({
                                url: '/project/lists',
                                method: 'GET',
                                success: function (data) {
                                    if (data.code === 0) {
                                        var projectSelect = $('#tbProject');
                                        projectSelect.empty(); // 清空之前的选项
                                        projectSelect.append('<option value="">请选择项目</option>'); // 默认选项
                                        data.data.forEach(function (projectObj) {
                                            projectSelect.append('<option value="' + projectObj.id + '">' + projectObj.projectName + '</option>');
                                        });
                                        var projectSelect1 = $('#importProject');
                                        projectSelect1.empty(); // 清空之前的选项
                                        projectSelect1.append('<option value="">请选择项目</option>'); // 默认选项
                                        data.data.forEach(function (projectObj) {
                                            projectSelect1.append('<option value="' + projectObj.id + '">' + projectObj.projectName + '</option>');
                                        });
                                        var projectSelect2 = $('#detProject');
                                        projectSelect2.empty(); // 清空之前的选项
                                        projectSelect2.append('<option value="">请选择项目</option>'); // 默认选项
                                        data.data.forEach(function (projectObj) {
                                            projectSelect2.append('<option value="' + projectObj.id + '">' + projectObj.projectName + '</option>');
                                        });
                                        form.render('select'); // 重新渲染 select 组件
                                        resolve(); // 数据加载完成后resolve
                                    } else {
                                        console.log('加载项目列表失败: ' + data.message);
                                    }
                                },
                                error: function (error) {
                                    reject(error);
                                }
                            });
                        });
                    }

                    // 表格渲染
                    table.render({
                        elem: '#tableList',
                        url: '/course/list',  // 替换成获取课程数据的接口
                        cols: [[
                            {
                                title: '序号', width: 80, templet: function (d) {
                                    return d.LAY_TABLE_INDEX + 1;
                                }
                            },
                            {field: 'projectName', title: '项目名称', width: 150},
                            {field: 'subject', title: '课程专题', width: 200},
                            {field: 'lessonDate', title: '上课日期', width: 120},
                            {field: 'week', title: '星期', width: 90},
                            {field: 'timeSpan', title: '时间段', width: 100},
                            {field: 'teacher', title: '教师', width: 150},
                            {field: 'address', title: '上课地点', width: 150},
                            {fixed: 'right', title: '操作', toolbar: '#barTable', width: 280}
                        ]],
                        page: true,
                        limit: 10, // 默认每页显示50条数据
                        limits: [10,50, 100, 150, 200],  // 可选择的每页显示条数
                        id: 'tableList',
                        done: function(e){
                            // 确保在删除和查询操作后执行以下代码
                            $('.layui-table').css("width", "100%");
                            $("th[data-field='delete']").css("border-right", 'none');
                        }
                    });

                    $('#search').click(function (event) {
                        event.preventDefault();  // 阻止表单默认提交

                        var project = $('#tbProject').val();  // 获取项目选择框的值
                        var date = $('#tbDate').val();  // 获取日期选择框的值

                        var searchButton = $(this);  // 获取查询按钮
                        searchButton.prop('disabled', true);  // 禁用查询按钮，防止多次点击
                        setTimeout(function () {
                            // 强制重新加载表格，确保每次查询都传递最新的参数
                            table.reload('tableList', {
                                url: '/course/list',  // 替换成课程查询接口
                                where: {
                                    projectId: project || '',  // 如果项目为空，传递空字符串
                                    lessonDate: date || ''  // 如果日期为空，传递空字符串
                                },
                                page: {
                                    curr: 1  // 强制从第一页开始查询
                                },
                                done: function (res, curr, count) {
                                    // 确保在删除和查询操作后执行以下代码
                                    $('.layui-table').css("width", "100%");
                                    $("th[data-field='delete']").css("border-right", 'none');
                                    searchButton.prop('disabled', false);  // 查询完成后启用查询按钮
                                    // layer.close(index);  // 关闭加载提示
                                    // 如果没有数据，显示提示
                                    if (count === 0) {
                                        layer.msg('没有符合条件的数据');
                                    }
                                }
                            });
                        }, 100);
                    });

                    // 监听操作按钮
                    table.on('tool(tableList)', function (obj) {
                        var data = obj.data;
                        if (obj.event == 'checkIn') {
                            // 模拟生成二维码，展示弹窗
                            var index = layer.open({
                                type: 1,
                                title: '签到二维码',
                                maxmin: true,
                                content: $('#qrCodeModal').html(),
                                area: ['350px', '385px'],
                                success: function () {
                                    // 后端生成二维码并返回图像链接
                                    getQRCode(data.id);  // 获取二维码并展示
                                },
                                end: function () {
                                    // 弹窗关闭时清除定时器，防止继续请求二维码
                                    clearInterval(qrCodeTimer);
                                }
                            });
                        } else if (obj.event == 'viewDetail') {
                            var index = layer.open({
                                type: 1,
                                title: '课程详情',
                                fixed: false,
                                maxmin: true,
                                content: $('#courseDetailModal').html(),
                                area: ['600px', '550px'],
                                btn: ['保存', '关闭'],
                                yes: function () {
                                    // 收集表单数据
                                    var courseData = {
                                        id: data.id, // 编辑时携带课程ID
                                        projectId: $('#detProject').val(),
                                        lessonDate: $('#lessonDate').val(),
                                        week: $('#week').val(),
                                        timeSpan: $('#timeSpan').val(),
                                        subject: $('#subject').val(),
                                        content: $('#content').val(),
                                        classType: $('#classType').val(),
                                        classMode: $('#classMode').val(),
                                        teacher: $('#teacher').val(),
                                        department: $('#department').val(),
                                        title: $('#title').val(),
                                        region: $('#region').val(),
                                        classHours: $('#classHours').val(),
                                        address:$('#address').val()
                                    };

                                    // 发送保存请求
                                    $.ajax({
                                        url: '/course/save',
                                        method: 'POST',
                                        contentType: 'application/json',
                                        data: JSON.stringify(courseData),
                                        success: function(res) {
                                            if(res.code == 0){
                                                layer.msg('保存成功');
                                                table.reload('tableList');
                                                layer.close(index);
                                            }else{
                                                layer.msg(res.msg || '保存失败');
                                            }
                                        }
                                    });
                                },
                                btn2: function () {
                                    layer.close(index);
                                },
                                success: function () {
                                    // 初始化上课日期选择器
                                    laydate.render({
                                        elem: '#lessonDate',
                                        type: 'date'  // 选择具体日期
                                    });
                                    loadProjectList().then(()=>{
                                        loadCourseDetail(data.id);
                                    });


                                }
                            });
                        } else if (obj.event == 'printQRCode') {
                            window.location.href = '/printQRCode/' + data.id;
                        } else if (obj.event == 'delete') {
                            layer.confirm('确定要删除吗？', {
                                btn: ['确定', '取消']
                            }, function (obj) {
                                // 确认删除后，执行删除操作
                                $.ajax({
                                    url: '/course/delete',
                                    data: {courseId: data.id},
                                    method: 'GET',
                                    success: function (response) {
                                        if (response.code == 0) {
                                            layer.msg("删除成功");
                                            console.log("ssssssssssss");
                                            table.reload('tableList', {
                                                done: function (res, curr, count) {
                                                    // 检查当前页是否还有数据
                                                    if (res.data.length === 0 && curr > 1) {
                                                        // 当前页没有数据且不是第一页，跳转到上一页
                                                        table.reload('tableList', {
                                                            page: {
                                                                curr: curr - 1
                                                            }
                                                        });
                                                    }
                                                    // 确保在删除和查询操作后执行以下代码
                                                    $('.layui-table').css("width", "100%");
                                                    $("th[data-field='delete']").css("border-right", 'none');
                                                }
                                            });
                                        } else {
                                            layer.msg('删除失败');
                                        }
                                    }
                                });
                            });
                        } else if (obj.event == 'export') {
                            var data = obj.data;
                           layer.open({
                                type: 1,
                                title: '课件管理',
                                maxmin: true,
                                content: $('#fileModal').html(),
                                area: ['525px', '200px'],
                                success: function() {
                                    loadCourseFiles(data.id);
                                    var index;
                                    // 初始化上传组件
                                    upload.render({
                                        elem: '#uploadBtn',
                                        url: '/course/uploadFile',  //上传多个文件
                                        accept: 'file',
                                        exts: 'pdf|doc|docx|ppt|pptx|xls|xlsx|jpg|png|zip|txt|rar|7z|mp4|avi|mkv|mov|jpeg|gif|webp',
                                        data: { courseId: data.id },
                                        before: function(obj) {
                                            index=layer.load();
                                        },
                                        done: function(res) {
                                            if(res.code === 0) {
                                                layer.msg('上传成功');
                                                loadCourseFiles(data.id); // 上传成功后重新加载课件列表
                                                layer.close(index);
                                            } else{
                                                layer.msg(res.msg);
                                                layer.close(index);
                                                // 加载课件列表
                                                loadCourseFiles(data.id);
                                            }
                                        }
                                    });

                                }
                            });
                        }
                    });

                    // 加载课件列表的函数
                    function loadCourseFiles(courseId) {
                        console.log(courseId)
                        $.ajax({
                            url: '/course/getCourseFiles',    //修改为获取到多个文件名字
                            data: { courseId: courseId },
                            method: 'GET',
                            success: function (response) {
                                if (response.code == 0) {
                                    var fileList = $('#fileList');
                                    fileList.empty(); // 清空之前的列表
                                    if (response.data.url_file && response.data.url_file.length > 0) {
                                        response.data.url_file.forEach(function (fileName) {
                                            files = fileName.replace(/^[\da-f]{8}-(?:[\da-f]{4}-){3}[\da-f]{12}_/, '');
                                            // console.log(files)
                                            // // uuid =fileName.split("_", 2)[0];
                                            // console.log(uuid)
                                            fileList.append('<tr>' +
                                                '<td>' + files + '</td>' +
                                                '<td>' + response.data.teacher + '</td>' +
                                                '<td><button class="layui-btn layui-btn-danger layui-btn-xs" data-file-id="' + fileName + '">删除</button></td>' +
                                                '</tr>');
                                        });
                                        fileList.find('.layui-btn-danger').on('click', function () {
                                            console.log($(this).data('file-id'))
                                            deleteFile(courseId, $(this).data('file-id'));
                                        });
                                    } else {
                                        fileList.append('<tr><td colspan="2">暂无课件</td></tr>');
                                    }
                                } else {
                                    layer.msg('获取课件信息失败');
                                }
                            },
                            error: function () {
                                layer.msg('获取课件信息失败');
                            }
                        });
                    }
                    function deleteFile(courseId,fileName) {
                        layer.confirm('确定要删除该课件吗？', {
                            btn: ['确定', '取消']
                        }, function (obj) {
                            console.log(fileName)
                            var index;
                            $.ajax({
                                url: '/course/deleteFile',
                                data: { courseId: courseId,fileName: fileName},
                                method: 'GET',
                                before: function (obj) {
                                 index=layer.load();
                                },
                                success: function (response) {
                                    console.log(response)
                                    if (response.code == 0) {
                                        layer.msg('删除成功');
                                        loadCourseFiles(courseId); //修改刷新
                                        layer.close(index);
                                    } else {
                                        layer.msg('删除失败');
                                    }
                                },
                                error: function () {
                                    layer.msg('删除失败');
                                }
                            });
                        });
                    }

                    // 模拟加载课程详情
                    function loadCourseDetail(courseId) {
                        $.ajax({
                            url: '/course/detail',  // 替换成获取课程详情的接口
                            data: {courseId: courseId},
                            method: 'GET',
                            success: function (response) {
                                if (response.code == 0) {
                                    var course = response.data;
                                    // 填充课程详情到弹窗对应字段
                                    // 填充项目名称
                                    console.log(course.projectId)
                                    $('#detProject').val(course.projectId);
                                    // 填充时间段
                                    console.log(course.timeSpan);
                                    $('#timeSpan').val(course.timeSpan);
                                    // 强制重新渲染表单
                                    form.render('select');
                                    // 填充其他字段
                                    $('#lessonDate').val(course.lessonDate);
                                    $('#week').val(course.week);
                                    console.log(course.subject)
                                    $('#subject').val(course.subject);
                                    $('#content').val(course.content);  // 内容要点
                                    $('#classType').val(course.classType);  // 课程类型
                                    $('#classMode').val(course.classMode);  // 培训方式
                                    $('#teacher').val(course.teacher);  // 授课教师
                                    $('#department').val(course.department);  // 授课教师单位
                                    $('#title').val(course.title);  // 职称
                                    // $('#famous').val(course.famous);  // 职称
                                    $('#region').val(course.region);  // 职称
                                    $('#classHours').val(course.classHours);  // 学时
                                    $('#address').val(course.address);
                                    // 重新渲染表单组件
                                    form.render();
                                } else {
                                    layer.msg('获取课程详情失败');
                                }
                            },
                            error: function () {
                                layer.msg('获取课程详情失败');
                            }
                        });
                    }

                    // 获取二维码并展示
                    var qrCodeTimer = null;  // 用于存储定时器

                    function getQRCode(courseId) {
                        if (qrCodeTimer) {
                            clearInterval(qrCodeTimer);  // 清除之前的定时器
                        }

                        $.ajax({
                            url: '/generate-qr-code',  // 后端生成二维码的接口
                            data: {courseId: courseId},
                            method: 'GET',
                            success: function (response) {
                                if (response.code == 0) {
                                    console.log('二维码数据:', response.data);  // 输出二维码数据
                                    var base64Data = response.data;
                                    if (base64Data) {
                                        $('#qrCodeImage').attr('src', 'data:image/png;base64,' + base64Data);
                                        startQRCodeAutoRefresh(courseId);  // 开始二维码自动刷新
                                    } else {
                                        layer.msg('二维码数据无效');
                                    }
                                } else {
                                    layer.msg('二维码生成失败');
                                }
                            },
                            error: function () {
                                console.log("失败")
                                layer.msg('生成二维码失败');
                            }
                        });
                    }

                    // 自动刷新二维码，每10秒更新一次
                    function startQRCodeAutoRefresh(courseId) {
                        qrCodeTimer = setInterval(function () {
                            getQRCode(courseId);  // 每10秒刷新一次二维码
                        }, 10000);  // 10秒刷新一次
                    }

                    var selectedFile = null; // 用于暂存上传的文件

                    // 批量导入按钮事件
                    $('#batchImport').click(function () {
                        layer.open({
                            type: 1,
                            title: '批量导入课程',
                            content: $('#batchImportModal').html(),
                            area: ['500px', '300px'],
                            success: function () {
                                // // 加载项目下拉框数据
                                // 初始化上课日期选择器
                                loadProjectList();

                                // 文件选择按钮事件
                                $('#chooseFile').click(function () {
                                    $('#importFile').click();
                                });

                                // 文件选择后显示文件名
                                $('#importFile').change(function () {
                                    selectedFile = this.files[0];
                                    $('#fileNameDisplay').text(selectedFile ? selectedFile.name : "未选择文件");
                                });

                                // 上传文件按钮事件
                                $('#uploadFile').click(function (e) {
                                    e.preventDefault();

                                    var projectId = $('#importProject').val();
                                    if (!projectId) {
                                        layer.msg('请选择项目！');
                                        return;
                                    }
                                    if (!selectedFile) {
                                        layer.msg('请先选择文件！');
                                        return;
                                    }

                                    // 构建 FormData 上传数据
                                    var formData = new FormData();
                                    formData.append('file', selectedFile);
                                    formData.append('projectId', projectId);

                                    // 显示加载动画
                                    var loadingIndex = layer.load(2, {shade: [0.3, '#fff']}); // 加载动画，2为类型，shade为遮罩透明度


                                    // 调用后端接口上传文件
                                    $.ajax({
                                        url: '/excel/import',
                                        type: 'POST',
                                        data: formData,
                                        contentType: false,
                                        processData: false,
                                        success: function (res) {
                                            layer.close(loadingIndex); // 隐藏加载动画
                                            layer.msg(res, {time: 1000}, function() { // 提示信息停留1秒（1000ms）
                                                layer.closeAll(); // 关闭弹窗
                                            });
                                            table.reload('tableList');
                                        },
                                        error: function () {
                                            layer.msg('上传失败，请重试！', {time: 1000});
                                        }
                                    });
                                });
                            }
                        });
                    });

                    $('#addNew').click(function () {
                        layer.open({
                            type: 1,
                            title: '新增课程',
                            fixed: false,
                            maxmin: true,
                            content: $('#courseDetailModal').html(),
                            area: ['600px', '550px'],
                            btn: ['保存', '关闭'],
                            offset: 'auto',
                            yes: function(index, layero){
                                // 收集表单数据
                                var courseData = {
                                    // id: data.id, // 编辑时携带课程ID
                                    projectId: $('#detProject').val(),
                                    lessonDate: $('#lessonDate').val(),
                                    week: $('#week').val(),
                                    timeSpan: $('#timeSpan').val(),
                                    subject: $('#subject').val(),
                                    content: $('#content').val(),
                                    classType: $('#classType').val(),
                                    classMode: $('#classMode').val(),
                                    teacher: $('#teacher').val(),
                                    department: $('#department').val(),
                                    title: $('#title').val(),
                                    region: $('#region').val(),
                                    classHours: $('#classHours').val(),
                                    address: $('#address').val()
                                };

                                // 发送保存请求
                                $.ajax({
                                    url: '/course/save',
                                    method: 'POST',
                                    contentType: 'application/json',
                                    data: JSON.stringify(courseData),
                                    success: function(res) {
                                        if(res.code == 0){
                                            layer.msg('保存成功');
                                            table.reload('tableList');
                                            layer.close(index);
                                        }else{
                                            layer.msg(res.msg || '保存失败');
                                        }
                                    }
                                });
                            },
                            btn2: function(index, layero){
                                layer.close(index);
                            },
                            success: function () {
                                // 初始化上课日期选择器
                                laydate.render({
                                    elem: '#lessonDate',
                                    type: 'date'  // 选择具体日期
                                });
                                // 加载项目下拉框数据
                                loadProjectList();
                                form.render();
                            }
                        });

                    });
                });

            </script>

        </div>
    </div>
</div>
</body>
</html>
